* ==============================================================================
*
* Date:	Summer 2024
* Paper: Climate shocks, regional favoritism and trust in leaders: Insights from droughts in Africa
*
* This creates Table 1			
*
* ==============================================================================

* Open dataset
cd $PATH
use tr_rep, clear

* Clean sample
keep if precision_code<=3
global zxcont "age age2 male ed_ord"
global zfecont "urban ea_svc_a ea_svc_b ea_svc_c ea_fac_a ea_fac_b ea_fac_c ea_fac_d ea_fac_e ea_sec_a ea_sec_b grs0 grsem0 grsl cropland_d ln_alt ln_dist_cit ln_alt_sd ln_dist_bor ln_dist_coa ln_dist_riv ln_dist_roa year"
global keyvars "tr_pres_ord aiclwmcgrwsp"

foreach var of varlist $zxcont $zfecont $keyvars {
	drop if `var'==.
}

* Standardize
egen z=std(aiclwmcgrwsp)
replace aiclwmcgrwsp=z
**#
drop z

**#
* Structuring
global sumvars "age male ed_ord aiclwmcgrwsp dry0y1 tr_pres_ord tr_pres tr_pres_some tr_pres_alot presregion capregion dsat cecon hndl_econ"
keep $sumvars
de $sumvars


label var dry0y1 "EMDAT/GDIS-drought Disaster in Region During Year t or t-1"
label var tr_pres_ord	"Trust in President"
label var tr_pres	"Trust in President: At Least a Little"
label var tr_pres_some	"Trust in President: At Least Somewhat"
label var tr_pres_alot	"Trust in President: A Lot"
label var presregion	"Lives in Leader Birth Region"
label var capregion	"Lives in Capital Region"
label var dsat "Satisfied with Democracy"
label var cecon	"Satisfied with Country's Economic Conditions"
label var hndl_econ "Satisfied with Economic Management"
label var aiclwmcgrwsp "Drought Conditions"
de $sumvars

global sumvars "age male ed_ord aiclwmcgrwsp tr_pres_ord presregion capregion"
keep $sumvars
de $sumvars



tempfile ordering
postfile handle str32 varname str244 varlabel using `ordering'

foreach v of varlist _all {
 post handle (`"`v'"') (`"`:var label `v''"')
}
postclose handle

preserve
use `ordering', clear
sort varlabel varname

gen order = ""
replace order = order[_n-1] + " " + varname
local desired = order[_N]

restore
order `desired'


de 
su


global tabname "summary"
estpost summarize 
**#
**#
esttab . using $OUTPATH/$tabname.rtf, title("Table 1. Summary statistics.") collabels(, lhs(\textbf{Variable})) cells("count(label(\textbf{N}) fmt(%7.0fc)) mean(label(\textbf{Mean}) fmt(%6.2f)) sd(label(\textbf{Std.dev}) fmt(%6.2f)) min(label(\textbf{Min}) fmt(%6.3f)) max(label(\textbf{Max}) fmt(%6.3f)) ") addnotes("{\i Notes:} This table provides summary statistics on respondents from Afrobarometer survey rounds 2-7 for which the location precision code is 1, 2, or 3, and for which all variables included in the baseline specification are non-missing.") varwidth(25) noobs nonumber nomtitles label compress nogaps replace 